 /*
 * dbDir Client
 */
package org.dbdir;

import java.io.BufferedReader;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.dbdir.core.Logger;
import org.dbdir.client.RespondHandler;
import org.dbdir.client.gui.MainFrame;
import org.dbdir.core.helper.ACL;
import org.dbdir.connections.TCPSocket;
import org.dbdir.core.helper.Database;

/**
 *
 * @author thotheolh
 */
public class Client {

    private TCPSocket sock = null;
    private BufferedReader in = null;
    private String fromServer;
    private String fromUser;
    private MainFrame frame;
    private Client current;
    private org.dbdir.api.client.Client api;
    private String response;
    private String username = null;
    private RespondHandler handler;
    private String currentDBProfile = ""; //current db profile
    private String accStr = ""; //accessory used string
    private Logger logger = new Logger();

    public Client() {
        current = this;
        api = new org.dbdir.api.client.Client();
        handler = api.getHandler();
        if (api.isConnected()) {
            init();
        } else {
            JOptionPane.showMessageDialog(frame, "Client could not find connection. \nPlease adjust the 'client.xml' configuraton with the correct configuration and restart the client.", "No Connection", JOptionPane.ERROR_MESSAGE);
            this.doExit();
        }
    }

    public boolean addUser(String username, String password) {
        return api.addUser(username, password);
    }

    public ArrayList getAvDBList() {
        return api.getAvDBList();
    }

    public boolean joindb(String dbalias) {
        return api.joindb(dbalias);
    }

    public boolean removeUser(String username, String dbalias) {
        return api.removeUser(username, dbalias);
    }

    public boolean delCurrentDB() {
        return api.deleteDbProfile(this.getCurrentDBProfile());
    }

    public void init() {
        setFrame(new MainFrame(this.getCurrent()));
        getFrame().setVisible(true);
        doLogin();
    }

    public void doExit() {
        if (api.endSession()) {
            System.exit(0);
        } else {
            System.exit(1);
        }
    }

    public void doLogin() {
        getFrame().getLoginPane();
    }

    public boolean doLoginProtocol(String username, String password) {
        api.doLogin(username, password);
        return api.isIsLogin();
    }

    public boolean doUpdatePersonal(String oldpass, String newpass) {
        return api.doUpdatePersonal(oldpass, newpass);
    }

    public ArrayList getUserDBList() {
        return api.getUserDBList();
    }

    public Database getDB(String dbname) {
        return api.getDbProfile(dbname);
    }

    /**
     * @return the current
     */
    public Client getCurrent() {
        return current;
    }

    public boolean isAdmin() {
        return api.isAdmin(this.getCurrentDBProfile());
    }

    public ACL getUserACL() {
        return api.getACL(this.getUsername(), this.getCurrentDBProfile());
    }

    public ACL getUserACL(String value) {
        return api.getACL(value, this.getCurrentDBProfile());
    }

    public boolean setUserAcl(ACL acl) {
        return api.updateACL(acl);
    }

    public ArrayList getDBUserList(String currentDBProfile) {
        return api.getDBUserList(currentDBProfile);
    }

    public boolean mkDBProfile(Database db) {
        return api.mkDBProfile(db);
    }

    public boolean removeMe() {
        return api.removeMe();
    }

    /**
     * @param current the current to set
     */
    public void setCurrent(Client current) {
        this.current = current;
    }

    /**
     * @return the frame
     */
    public MainFrame getFrame() {
        return frame;
    }

    /**
     * @param frame the frame to set
     */
    public void setFrame(MainFrame frame) {
        this.frame = frame;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the currentDBProfile
     */
    public String getCurrentDBProfile() {
        return currentDBProfile;
    }

    /**
     * @param currentDBProfile the currentDBProfile to set
     */
    public void setCurrentDBProfile(String currentDBProfile) {
        this.currentDBProfile = currentDBProfile;
    }

    public boolean updateDBProfile(Database db) {
        return api.updateDBProfile(db);
    }

    /**
     * @return the accStr
     */
    public String getAccStr() {
        return accStr;
    }

    /**
     * @param accStr the accStr to set
     */
    public void setAccStr(String accStr) {
        this.accStr = accStr;
    }

    /**
     * @return the handler
     */
    public RespondHandler getHandler() {
        return handler;
    }

    /**
     * @param handler the handler to set
     */
    public void setHandler(RespondHandler handler) {
        this.handler = handler;
    }

}
